package com.contory.ssn_lgnd.app.title;

import java.util.ArrayList;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.Pool;
import com.contory.ssn_lgnd.framework.Pool.PoolObjectFactory;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;

public class TitleWaveObject implements DrawPtr, UpdatePtr{
	PoolObjectFactory<WaveObjectChd> factory = new PoolObjectFactory<WaveObjectChd>() {
        public WaveObjectChd createObject() {
            return new WaveObjectChd();
        }
    };
	Pool<WaveObjectChd> viewPool = new Pool<WaveObjectChd>(factory, 10);
	ArrayList<WaveObjectChd> waveChdObjList = new ArrayList<WaveObjectChd>();
	
	float beginX;
	float beginY;
	float beginW;
	float beginH;
	float creatPos = 3.0f;
	float endPos = 0;
	float velocity = 0.5f;
	float wIncRate = 0.5f;
	float hIncRate = 0.01f;
	
	public TitleWaveObject(){
		//10, 4.0f, 10, 0.05f, 0.247f, 0.282f, 0.8f, 0.2f, Assets.tex_reg_whiteTexel
		beginX = 10;
		beginY = 4;
		beginW = 10;
		beginH = 0.05f;
		WaveObjectChd goChd = viewPool.newObject();
		goChd.initialize(beginX, beginY, beginW, beginH, 0.247f, 0.282f, 0.8f, 0.2f, Assets.tex_reg_whiteTexel, false);
		waveChdObjList.add(goChd);
	}
	
	@Override
	public void update(float deltaTime){
		for (int i = 0; i < waveChdObjList.size(); i++) {
			WaveObjectChd goChd = waveChdObjList.get(i);
			goChd.y -= deltaTime * velocity;
			goChd.w += deltaTime * wIncRate;
			goChd.h += deltaTime * hIncRate;
			if(goChd.y < creatPos && !goChd.isCreated){
				goChd.isCreated = true;
				WaveObjectChd creatGochd = viewPool.newObject();
				creatGochd.initialize(beginX, beginY, beginW, beginH, goChd.r, goChd.g, goChd.b, goChd.a, goChd.tr, false);
				waveChdObjList.add(creatGochd);
			}
			
			if(goChd.y < endPos){
				viewPool.free(goChd);
				waveChdObjList.remove(goChd);
			}
		}
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		for (int i = 0; i < waveChdObjList.size(); i++) {
			WaveObjectChd goChd = waveChdObjList.get(i);
			goChd.draw(batcher);
		}
	}
	
	class WaveObjectChd extends ViewObject implements DrawPtr{
		boolean isCreated = false;
		public void initialize(float x, float y, float w, float h, float r, float g, float b, float a, TextureRegion tr, boolean isCreated){
			this.setXY_WH_TR(x, y, w, h, tr);
			this.setRGBA(r, g, b, a);
			this.isCreated = isCreated;
		}
		
		@Override
		public void draw(SpriteBatcher batcher){
			batcher.drawSprite(x, y, w, h, r, g, b, a, tr);
		}
	}
}
